package com.handinfo.android.ui.window;

import java.io.DataInputStream;
import java.io.IOException;

import android.graphics.Bitmap;
import android.graphics.Color;

import com.handinfo.android.DWGameManager;
import com.handinfo.android.core.graphics.DWFont;
import com.handinfo.android.core.graphics.DWGraphics;
import com.handinfo.android.core.net.GameProtocol;
import com.handinfo.android.game.IIcon;
import com.handinfo.android.game.IconManger;
import com.handinfo.android.ui.IUIWindows;
import com.handinfo.android.ui.UIWindows;
import com.handinfo.android.uicontrols.DWControlsManager;
import com.handinfo.android.uicontrols.DWListener;
import com.handinfo.android.uicontrols.controls.DWBackground;
import com.handinfo.android.uicontrols.controls.DWButton;
import com.handinfo.android.uicontrols.controls.DWFrame;
import com.handinfo.android.uicontrols.controls.DWGrid;
import com.handinfo.android.uicontrols.controls.DWImageBox;
import com.handinfo.android.uicontrols.controls.DWTextbox;
import com.handinfo.android.uicontrols.controls.DWTitle;
import com.handinfo.android.utils.Tools;

public class UIFirstRecharge implements IUIWindows
{
	private Bitmap img_title1;
	private Bitmap img_close;
	private Bitmap img_message1;
	private Bitmap img_message2;
	private Bitmap img_message3;
	private Bitmap img_caishen;
	private Bitmap img_title2;
	private Bitmap img_brackground;
	private Bitmap img_wuping;
	private Bitmap img_button;
	private Bitmap img_anxia;
	private Bitmap img_tishi;
	private Bitmap img_chongzhi;
	
	private Bitmap img_background1;
	private Bitmap img_background2;
	
	private long m_role_id;
	
	private String str_title[] = {
			"首次充值5元礼包",
			"首次充值15元礼包"
			};
	
	public void init()
	{
		img_title1 = UIWindows.createImage("/img/newui/shouchongjiangli_1.gnp");
		img_close = UIWindows.createImage("/img/newui/fanhuianniu_1.gnp");// 关闭窗口图标
		img_message1 = UIWindows.createImage("/img/newui/shouchongzi_1.gnp");
		img_message2 = UIWindows.createImage("/img/newui/shouchongzi_2.gnp");
		img_message3 = UIWindows.createImage("/img/newui/shouchongzi_3.gnp");
		img_caishen = UIWindows.createImage("/img/newui/caishendt_1.gnp"); // 图太大，缩小
		img_title2 = UIWindows.createImage("/img/newui/tiao.gnp");
		img_brackground = UIWindows.createImage("/img/newui/beibaobj_1.gnp");
		img_wuping = UIWindows.createImage("/img/newui/wupinlan.gnp");
		img_button = UIWindows.createImage("/img/newui/anniu_4.gnp");
		img_anxia = UIWindows.createImage("/img/newui/anniu_4ax.gnp");
		img_tishi = UIWindows.createImage("/img/newui/shouchongbt_1.gnp");
		img_chongzhi = UIWindows.createImage("/img/newui/chongzhi_1.gnp");
		
		img_background1 = UIWindows.createImage("/img/newui/beibaobj_2.gnp");
		img_background2 = UIWindows.createImage("/img/newui/xiuluoshachangbj_1.gnp");
	}

	private DWFrame m_frame;
	private DWTitle m_title;
	private DWImageBox m_titleImg;
	private DWImageBox m_close;
	private DWImageBox m_message1;
	private DWImageBox m_message2;
	private DWImageBox m_message3;
	
	private DWBackground m_caishen_background;
	private DWBackground m_item_background[] = new DWBackground[2];
	private DWImageBox m_item_title[] = new DWImageBox[2];
	private DWTextbox m_item_msg[]=new DWTextbox[2];
	private DWGrid m_itemGrid[][]= new DWGrid[2][];
	private DWTextbox m_itemText[][]=new DWTextbox[2][];
	private DWButton m_button;
	private DWImageBox m_chongzhi;
	
	private DWBackground m_win_background1;
	private DWBackground m_win_background2;
	
	public void open(byte index)
	{
		if(!DWControlsManager.getInstance().contains(m_frame))
			initFrame();
		
//		addRechargeGrid();
		DWControlsManager.getInstance().addControl(m_frame);
		///更新界面		
	}

	/**
	 * 初始化界面
	 */
	private void initFrame()
	{
		m_frame = new DWFrame(DWFrame.FRAME_TYPE_FULL);
		
		m_win_background1 = new DWBackground(img_brackground, m_frame.getShowWidth(), m_frame.getShowHeight());
		m_win_background1.setNearAnchor(m_frame, Tools.HCENTER_VCENTER, Tools.HCENTER_VCENTER);
		m_frame.addControl(m_win_background1);
		
		m_win_background2 = new DWBackground(img_background2, m_frame.getShowWidth(), m_frame.getShowHeight());
		m_win_background2.setNearAnchor(m_frame, Tools.HCENTER_VCENTER, Tools.HCENTER_VCENTER);
		m_frame.addControl(m_win_background2);
		
		m_title = new DWTitle("", m_frame);
		m_frame.addControl(m_title);
		m_frame.setBackgroundColor(Tools.ALPHA);
		
		m_titleImg = new DWImageBox(img_title1);
		m_titleImg.setNearAnchor(m_title, Tools.HCENTER_VCENTER, Tools.HCENTER_VCENTER);
		m_frame.addControl(m_titleImg);
		
		m_close = new DWImageBox(img_close);
		m_close.setNearAnchor(m_title, Tools.RIGHT_VCENTER, Tools.RIGHT_VCENTER);
		m_close.setTouchZoomIn(UIFixWindow.TOUCHZOOM, UIFixWindow.TOUCHZOOM);
		m_close.addListener(new DWListener() {
			
			public void OnDoubleClick()
			{
			}
			
			public void OnClick()
			{
				close((byte) 0);
			}
		});
		m_frame.addControl(m_close);
		
		m_message1 = new DWImageBox(img_message1);
		m_message1.setNearAnchor(m_title, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, 60,10);
		m_frame.addControl(m_message1);
		
		m_message2 = new DWImageBox(img_message2);
		m_message2.setNearAnchor(m_message1, Tools.LEFT_TOP, Tools.LEFT_BOTTOM);
		m_frame.addControl(m_message2);
		
		m_message3 = new DWImageBox(img_message3);
		m_message3.setTop(true);
		m_message3.setNearAnchor(m_title, Tools.RIGHT_TOP, Tools.RIGHT_BOTTOM, -40,10 + img_message1.getHeight() + img_message2.getHeight() - img_message3.getHeight()/3);
		m_frame.addControl(m_message3);
		
		m_caishen_background = new DWBackground(m_frame.getShowWidth()/3, img_caishen.getHeight()* (m_frame.getShowWidth()/3) / img_caishen.getWidth());
		m_caishen_background.setBackground(img_caishen);
		m_caishen_background.setNearAnchor(m_message3, Tools.RIGHT_TOP, Tools.RIGHT_BOTTOM , 10,0);
		m_frame.addControl(m_caishen_background);
		
		m_item_background[0] = new DWBackground(img_brackground,img_wuping.getWidth()*20/3 + 80,img_wuping.getHeight() + img_title2.getHeight());
		m_item_background[0].setNearAnchor(m_message2, Tools.LEFT_TOP, Tools.LEFT_BOTTOM,10,20);
		m_frame.addControl(m_item_background[0]);
		
		m_item_title[0] = new DWImageBox(img_title2);
		m_item_title[0].setNearAnchor(m_item_background[0], Tools.LEFT_TOP, Tools.LEFT_TOP,0,-img_title2.getHeight()/3);
		m_frame.addControl(m_item_title[0]);
		
		m_item_msg[0]=new DWTextbox(str_title[0]);
		m_item_msg[0].setNearAnchor(m_item_title[0], Tools.LEFT_VCENTER, Tools.LEFT_VCENTER, 40, 0);
		m_frame.addControl(m_item_msg[0]);
		
		m_item_background[1] = new DWBackground(img_brackground,m_item_background[0].getShowWidth(),m_item_background[0].getShowHeight());
		m_item_background[1].setNearAnchor(m_item_background[0], Tools.LEFT_TOP, Tools.LEFT_BOTTOM,0,10 + img_title2.getHeight()/3);
		m_frame.addControl(m_item_background[1]);
		
		m_item_title[1] = new DWImageBox(img_title2);
		m_item_title[1].setNearAnchor(m_item_background[1], Tools.LEFT_TOP, Tools.LEFT_TOP,0,-img_title2.getHeight()/3);
		m_frame.addControl(m_item_title[1]);
		
		m_item_msg[1]=new DWTextbox(str_title[1]);
		m_item_msg[1].setNearAnchor(m_item_title[1], Tools.LEFT_VCENTER, Tools.LEFT_VCENTER, 40, 0);
		m_frame.addControl(m_item_msg[1]);
		
		m_button = new DWButton("", img_button);
		m_button.setDownImage(img_anxia);
		m_button.setNearAnchor(m_frame, Tools.RIGHT_BOTTOM, Tools.RIGHT_BOTTOM , -20 , -20);
		m_button.addListener(new DWListener() {
			
			public void OnDoubleClick()
			{
			}
			
			public void OnClick()
			{
				close((byte) 0);
				UIWindows.getInstance().m_recharge.open((byte) 0);
			}
		});
		m_frame.addControl(m_button);
				
		m_chongzhi = new DWImageBox(img_chongzhi);
		m_chongzhi.setNearAnchor(m_button, Tools.HCENTER_VCENTER, Tools.HCENTER_VCENTER);
		m_chongzhi.setTouch(false);
		m_frame.addControl(m_chongzhi);
	}
	
	public void close(byte index)
	{
		DWControlsManager.getInstance().removeControl(m_frame);
	}

	public void dispose()
	{
	}

	public void logic()
	{
	}
	private RechargeItem m_item[][]= new RechargeItem[2][]; //
	/**
	 * 刷新道具
	 * @param item
	 * @param index
	 */
	private void addItem(final byte index)
	{
		if(index > 1 || m_item[index] == null)
			return;
		
		cleanItem(index);
		
		m_itemGrid[index] = new DWGrid[m_item[index].length];
		m_itemText[index] =new DWTextbox[m_item[index].length];
		
		for (int i = 0; i < m_item[index].length; i++) {
			m_itemGrid[index][i] = new DWGrid(img_wuping);
			m_itemGrid[index][i].addObject(m_item[index][i]);
			final int flag = i;
			m_itemGrid[index][i].addListener(new DWListener() {				
				public void OnDoubleClick()
				{
				}
				
				public void OnClick()
				{
					DWGameManager.getInstance().getSendMessage().sendSeeItemInfos(0	, m_item[index][flag].m_key);
				}
			});
			if(i == 0)
			{
				m_itemGrid[index][i].setNearAnchor(m_item_background[index], Tools.LEFT_BOTTOM, Tools.LEFT_BOTTOM, 40, -5);
			}
			else
			{
				m_itemGrid[index][i].setNearAnchor(m_itemGrid[index][i-1], Tools.LEFT_VCENTER, Tools.RIGHT_VCENTER, img_wuping.getWidth()*2/3+5,0);
			}
			m_frame.addControl(m_itemGrid[index][i]);
			
			m_itemText[index][i] = new DWTextbox("x"+m_item[index][i].m_num);
			m_itemText[index][i].setNearAnchor(m_itemGrid[index][i], Tools.LEFT_BOTTOM, Tools.RIGHT_BOTTOM, 5, 0);
			m_frame.addControl(m_itemText[index][i]);
		}		
	}
	
	/**
	 * 清理控件
	 * @param index
	 */
	
	private void cleanItem(byte index)
	{
		if(m_itemGrid[index] ==null || m_itemText[index] ==null)
			return;
		
		for(int i=0;i<m_itemGrid[index].length;i++)
		{
			m_frame.removeControl(m_itemGrid[index][i]);
			m_frame.removeControl(m_itemText[index][i]);			
		}
	}
	
	/**
	 * 首充领奖详情
	 * @param dis
	 */
	public void recvRechargeItem(DataInputStream dis)
	{
		try {
			for(int i=0;i<2;i++)
			{
				int size=dis.readByte();
				m_item[i]=new RechargeItem[size];
				Tools.debugPrintln("item长度"+i+"=" + size);
				for(int j=0;j < size; j++)
				{
					m_item[i][j] = new RechargeItem();
					m_item[i][j].recvItemFormServer(dis);
				}
				addItem((byte) i);				
			}			
		}
		catch (Exception e) {
			Tools.debug(e);
		}
	}
	
	/**
	 *首充提示
	 * @param dis
	 */
	public void recvRechargeTishi(DataInputStream dis)
	{
		try {		
			addRechargeGrid();
		}
		catch (Exception e) {
			Tools.debug(e);
		}
	}
	

	/**
	 * 主界面上绘制提示图标
	 */
	private DWImageBox m_tishi;
	public void addRechargeGrid()
	{
		m_role_id = DWGameManager.getInstance().m_role.m_gameId;
		
		final UIMain uimain= UIWindows.getInstance().getMain();
		
		if(uimain.m_Uimain_Frame.contains(m_tishi))
			return;
		
		if(m_tishi == null)
		{
			m_tishi = new DWImageBox(img_tishi);
			m_tishi.setNearAnchor(uimain.m_imageBox_yunying,  Tools.LEFT_VCENTER,Tools.RIGHT_VCENTER, 10, 0);
			m_tishi.setSelectType(DWImageBox.SELECT_TYPE_ARC3);
			m_tishi.setSelect(true);
			m_tishi.setTop(true);
			
			m_tishi.addListener(new DWListener() {			
				public void OnDoubleClick()
				{
				}
				
				public void OnClick()
				{
//					uimain.m_Uimain_Frame.removeControl(m_tishi);
					open((byte) 0);
					DWGameManager.getInstance().getSendMessage().sendFirstRechargeInfo();
				}
			});
		}	
		
		uimain.m_Uimain_Frame.addControl(m_tishi);
	}
	
	/**
	 * 抽奖道具类
	 * @author Administrator
	 * 
	 */
	class RechargeItem implements IIcon
	{
		public Bitmap m_icon;

		public String m_key;
		public short m_iconId;//icon
		public byte m_num; //数量

		public void recvItemFormServer(DataInputStream dis) throws IOException
		{
			m_key = dis.readUTF();
			Tools.debugPrintln("item 名称" + m_key);
			m_num = dis.readByte();
			Tools.debugPrintln("item 数量" + m_num);
			m_iconId = dis.readShort();
			Tools.debugPrintln("item 图标id" + m_iconId);
			m_icon = IconManger.getInstance().getIcon(m_iconId);
			
		}

		public void drawIcon(DWGraphics g, int x, int y, int w, int h, int anchor)
		{
			if (m_icon != null) {
				g.drawBitmap(m_icon, x, y, anchor);
			}
		}

		public void drawIcon(DWGraphics g, int x, int y, int w, int h, float scaleX, float scaleY, int anchor)
		{
		}
	}
	
	/**
	 * 图标显示
	 */
	public void setIcon()
	{
		if(m_tishi == null)
			return;
		
		if( UIWindows.getInstance().getMain().m_Uimain_Frame.contains(m_tishi))
		{
			if(m_role_id != DWGameManager.getInstance().m_role.m_gameId)
				UIWindows.getInstance().getMain().m_Uimain_Frame.removeControl(m_tishi);

		}		
	}
	
	/**
	 *首充后移除图标
	 * @param dis
	 */
	public void recvRechargeRemove(DataInputStream dis)
	{
		try {	
			UIWindows.getInstance().getMain().m_Uimain_Frame.removeControl(m_tishi);
		}
		catch (Exception e) {
			Tools.debug(e);
		}
	}
	
	
	
}
